package com.autoEcole.service;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

import com.autoEcole.dao.VehiculeDAO;
import com.autoEcole.exception.ApplicationException;
import com.autoEcole.exception.DAOException;
import com.autoEcole.model.Vehicule;

public class VehiculeService {
	
	private VehiculeDAO vehiculeDAO;
	/**
	 * VehiculeService
	 */
	private static VehiculeService vehiculeService;
	
	/**
	 * Constructor
	 */
	private VehiculeService() {
		super();
	}

	/**
	 * Returns the VehiculeService instance (Design Pattern : Singleton)
	 * 
	 * @return VehiculeService
	 */
	public static synchronized VehiculeService getInstance() {
		if (vehiculeService == null)
			vehiculeService = new VehiculeService();
		return vehiculeService;
	}

	public VehiculeDAO getVehiculeDAO() {
		return vehiculeDAO;
	}

	public void setVehiculeDAO(VehiculeDAO vehiculeDAO) {
		this.vehiculeDAO = vehiculeDAO;
	}

	public int create(Vehicule vehicule) throws ApplicationException {
		try {
			return vehiculeDAO.create(vehicule);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * 
	 */
	public void update(Vehicule vehicule) throws ApplicationException {
		try {
			vehiculeDAO.update(vehicule);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * retourne l'element si il existe ou null
	 */
	public Vehicule findById(int id) throws ApplicationException {
		try {
			return vehiculeDAO.findById(id);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}

	/**
	 * @throws ParseException 
	 * 
	 */
	public List<Vehicule> findAll() throws ApplicationException, ParseException {
		try {
			return vehiculeDAO.findAll();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
	
	/**
	 * 
	 */
	public Map<Integer , Vehicule> findAllMap() throws ApplicationException {
		try {
			return vehiculeDAO.findAllMap();
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}
	}
	
	/**
	 * 
	 */	
	public void delete(int vehiculeId) throws ApplicationException {
		try {
			vehiculeDAO.delete(vehiculeId);
		} catch (DAOException ex) {
			throw new ApplicationException(ex);
		}		
	}
}
